package com.edu.zql;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.CountDownLatch;

/**
 * Created by User on 2017/10/13.
 */
public class 单例 {

    public static void main(String[] args) throws InterruptedException {
        int threadNum = 20;

        CountDownLatch countDownLatch = new CountDownLatch(1);
        CountDownLatch end = new CountDownLatch(threadNum);

        final Set list = new CopyOnWriteArraySet();

        for (int i = 0; i < threadNum; i++) {
            new Thread(()->{
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                list.add(SingleInstance5.getInstance0());
                end.countDown();
            }).start();
        }



        countDownLatch.countDown();


        end.await();

        System.out.println(list.size());
        for (Object obj : list) System.out.println(obj);

    }



    //饿汉式
    static class SingleInstance0{

        static SingleInstance0 instance = new SingleInstance0();

        public static SingleInstance0 getInstance() {
            return instance;
        }
    }

    //懒汉式0
    static class SingleInstance1{

        static SingleInstance1 instance = null;

        static SingleInstance1 getInstance() {
            if (instance == null)
                instance = new SingleInstance1();

            return instance;
        }

    }

    //懒汉式1
    static class SingleInstance2 {

        static SingleInstance2 instance = null;

        static SingleInstance2 getInstance() {
            synchronized (SingleInstance2.class) {
                if (instance == null)
                    instance = new SingleInstance2();
            }

            return instance;
        }

    }

    //懒汉式2
    static class SingleInstance3 {

        static SingleInstance3 instance = null;

        static SingleInstance3 getInstance() {
            if (instance == null) {
                synchronized (SingleInstance3.class) {
                    if (instance == null)
                        instance = new SingleInstance3();
                }
            }

            return instance;
        }

    }

    //静态内置类
    static class SingleInstance4 {

        static SingleInstance4_0 getInstance0() {
            return SingleInstance4_0.singleInstance4_0;
        }
        static SingleInstance4_1 getInstance1() {
            return SingleInstance4_1.singleInstance4_1;
        }

        static class SingleInstance4_0 {
            static SingleInstance4_0 singleInstance4_0 = new SingleInstance4_0();
        }
        static class SingleInstance4_1 {
            static SingleInstance4_1 singleInstance4_1 = new SingleInstance4_1();
        }

    }

    //枚举数据类型实现
    static class SingleInstance5 {

        static Object getInstance0() {
            return Single.single0.getInstance();
        }

         static Object getInstance1() {
            return Single.single1.getInstance();
        }



        enum Single {
            single0,single1;

            Object object;

            Single() {
                object = new Object();
            }

            public Object getInstance() {
                return object;
            }
        }

    }




}
